package com.ruoyi.fztz.lstb.xmls.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.fztz.lstb.xmls.domain.LstbXmls;
import com.ruoyi.fztz.lstb.xmls.domain.LstbXmlsFj;
import com.ruoyi.fztz.lstb.xmls.service.ILstbXmlsService;
import com.ruoyi.fztz.lstb.xmls.vo.YbFyVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * 项目流水Controller
 *
 * @author huacai
 * @date 2024-11-19
 */
@RestController
@RequestMapping("/fztz/xmls")
public class LstbXmlsController extends BaseController {
    @Resource
    private ILstbXmlsService lstbXmlsService;

    /**
     * 查询项目流水列表
     */
    @GetMapping("/list")
    public TableDataInfo list(LstbXmls lstbXmls) {
        startPage();
        List<LstbXmls> list = lstbXmlsService.selectLstbXmlsList(lstbXmls);
        return getDataTable(list);
    }

    /**
     * 导出项目流水列表
     */
    @Log(title = "项目流水", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LstbXmls lstbXmls) {
        List<LstbXmls> list = lstbXmlsService.selectLstbXmlsList(lstbXmls);
        ExcelUtil<LstbXmls> util = new ExcelUtil<LstbXmls>(LstbXmls.class);
        util.exportExcel(response, list, "项目流水数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<LstbXmls> util = new ExcelUtil<LstbXmls>(LstbXmls.class);
        util.importTemplateExcel(response, "项目流水数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "项目流水", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<LstbXmls> util = new ExcelUtil<LstbXmls>(LstbXmls.class);
        InputStream inputStream = file.getInputStream();
        List<LstbXmls> list = util.importExcel(inputStream);
        inputStream.close();
        int count = lstbXmlsService.batchInsertLstbXmls(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取项目流水详细信息
     */
    @GetMapping(value = "/{xmlsId}")
    public AjaxResult getInfo(@PathVariable("xmlsId") String xmlsId) {
        return success(lstbXmlsService.selectLstbXmlsByXmlsId(xmlsId));
    }

    /**
     * 新增项目流水
     */
    @Log(title = "项目流水", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LstbXmls lstbXmls) {
        lstbXmls.setUserId(Math.toIntExact(getUserId())); //用户ID
        lstbXmls.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        lstbXmls.setCreateBy(getUsername()); //创建人
        lstbXmls.setIdWhere("存在");
        return toAjax(lstbXmlsService.insertLstbXmls(lstbXmls));
    }

    /**
     * 修改项目流水
     */
    @Log(title = "项目流水", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LstbXmls lstbXmls) {
        return toAjax(lstbXmlsService.updateLstbXmls(lstbXmls));
    }

    /**
     * 删除项目流水
     */
    @Log(title = "项目流水", businessType = BusinessType.DELETE)
    @DeleteMapping("/{xmlsIds}")
    public AjaxResult remove(@PathVariable String[] xmlsIds) {
        return toAjax(lstbXmlsService.deleteLstbXmlsByXmlsIds(xmlsIds));
    }

    /**
     * 获得项目流水主键ID (UUID)
     */
    @GetMapping("/getXmLsId")
    public AjaxResult getXmLsId() {
        String getXmLsId = IdUtils.fastSimpleUUID();
        return success(getXmLsId);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertXmlsFjByXmlsId(@RequestBody LstbXmlsFj lstbXmlsFj) {
        return toAjax(lstbXmlsService.insertXmlsFjByXmlsId(lstbXmlsFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectXmlsFjList(String sId) {
        LstbXmlsFj lstbXmlsFj = new LstbXmlsFj();
        lstbXmlsFj.setXmlsId(sId);
        return AjaxResult.success(lstbXmlsService.selectXmlsFjList(lstbXmlsFj));
    }

    /**
     * 删除项目流水附件
     */
    @Log(title = "项目流水附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult deleteLstbXmlsFjByFjIds(@PathVariable Integer[] fjIds) {
        return toAjax(lstbXmlsService.deleteLstbXmlsFjByFjIds(fjIds));
    }

    /**
     * 获取费用月报
     * @param xmsbId 项目申报ID
     * @param nd 年度
     * @param sdZt 锁定状态
     * @return 费用月报
     */
    @GetMapping(value = "/selectYbFy")
    public AjaxResult selectYbFyList(Integer xmsbId, String nd, String sdZt) {
        YbFyVo ybFyVo = new YbFyVo();
        ybFyVo.setXmsbId(xmsbId);
        ybFyVo.setNd(nd);
        ybFyVo.setSdZt(sdZt);
        return AjaxResult.success(lstbXmlsService.selectYbFyList(ybFyVo));
    }


}
